<?

require_once('DataObject/DomainModel.php');

class Languages extends DomainModel {
	protected $id;	//primaryKey=yes, dataType=smallint, maxLength=, allowNulls=no
	protected $language;	//primaryKey=no, dataType=varchar, maxLength=30, allowNulls=no
	protected $shortcut;	//primaryKey=no, dataType=char, maxLength=10, allowNulls=no
	
	protected $config;
	
	public function getConfig(){
		if (is_null($this->config)){
			$registry = Zend_Registry::getInstance();
			$config = $registry->get('config');
			$this->config = $config->locale;
		}
		
		return $this->config;
	}
	
	function registerDir(){
		//echo $_SERVER['DOCUMENT_ROOT'].$this->getConfig()->localeDir.'/'.trim($this->shortcut);
		if (!is_dir($_SERVER['DOCUMENT_ROOT'].'/'.$this->getConfig()->localeDir.'/'.trim($this->shortcut))){
			mkdir($_SERVER['DOCUMENT_ROOT'].'/'.$this->getConfig()->localeDir.'/'.trim($this->shortcut), 777);
		}
	}
	
	function removeDir(){
		if (is_dir($_SERVER['DOCUMENT_ROOT'].'/'.$this->getConfig()->localeDir.'/'.trim($this->shortcut))){
			rmdir($_SERVER['DOCUMENT_ROOT'].'/'.$this->getConfig()->localeDir.'/'.trim($this->shortcut));
		}
	}
}




/**
 * Class for DB_Object ModelMapper
 *
 * @category   RFC_Model
 * @package    RFC_DModel
 * @copyright  Copyright (c) 2010-2011 Rapid Code Factory Ltd. POLAND (http://www.rapidcodefactory.com)
 * @license    http://suite.rapidcodefactory.com/license/new-bsd     New BSD License
*/
class LanguagesMapper extends ModelMapper {
	function __construct(){
		parent::__construct();
		$this->tableName = 'languages';
		$this->selectViewName = 'v_languages';
		$this->selectFields = '
				id,
				language,
				shortcut
		';

		$this->findQuery = self::$DB->prepare("
			SELECT ".$this->selectFields."
			FROM ".$this->selectViewName."
			WHERE id = ?
		");
		$this->insertQuery = self::$DB->prepare("
			CALL p_add_languages (@p_id,
				?,
				?
		)");


		$this->updateQuery = self::$DB->prepare("
			CALL p_upd_languages (
				?,
				?,
				?

		)");

		$this->deleteQuery = self::$DB->prepare("
			CALL p_del_languages (?)
		");

		$this->cacheTime = null;
	}

	protected function doLoad($array){
		$acuire = str_replace('Mapper', '', get_class($this));
		$obj = new $acuire($array['id']);
		$obj->setId($array['id']);
		$obj->setLanguage($array['language']);
		$obj->setShortcut($array['shortcut']);

		return $obj;
	}

	function insert(DomainModel $obj){

		$values = array(
			$obj->getLanguage(),
			$obj->getShortcut()
		);

		$this->doStatement($this->insertQuery, $values);
		$obj->setId(self::$DB->getOne("SELECT @p_id"));
		$obj->registerDir();
	}

	function update(DomainModel $obj){
		$values = array(
			$obj->getId(),
			$obj->getLanguage(),
			$obj->getShortcut()
		);

		$this->doStatement($this->updateQuery, $values);
		$obj->registerDir();
	}

	function delete(DomainModel $obj){
		$this->doStatement($this->deleteQuery, array($obj->getId()));
		$obj->removeDir();
	}

	function newId(){

		$row = self::$DB->getRow('SHOW TABLE STATUS LIKE languages');
		return $row['Auto_increment'];

	}
}

class LanguagesCollection extends ModelCollection {
	function add(Languages $model){
		$this->doAdd($model);
	}
}

class LanguagesCollectionExtended extends LanguagesCollection {
	private $stmt;
	private $valueArray;
	private $mapper;
	private $run = false;

	function __construct(ModelMapper $mapper, $stmtHandle, $valueArray){
		parent::__construct();
		$this->stmt = $stmtHandle;
		$this->valueArray = $valueArray;
		$this->mapper = $mapper;
	}

	function notifyAccess(){
		if (!$this->run){
			$result = $this->mapper->doStatement($this->stmt, $this->valueArray,$this->mapper->getCacheTime());
			$this->init_db($result, $this->mapper);
			$this->run = true;
		}
	}
}

?>